package com.medicine.manage.block.service.impl;

import com.medicine.manage.block.WeBASEUtils;
import com.medicine.manage.block.entity.WeBaseResult;
import com.medicine.manage.block.service.IBlockOrderService;
import com.medicine.manage.block.service.IUserExtendService;
import com.medicine.manage.domain.Order;
import com.medicine.manage.service.IOrderService;
import com.medicine.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 区块链订单管理Service业务层处理
 *
 * @author admin
 * @date 2024-10-12
 */
@Service
public class BlockOrderServiceImpl implements IBlockOrderService {
    // 智能合约插入订单方法名
    private static final String INSERT_ORDER = "insertOrder";
    // 智能合约更新订单方法名
    private static final String UPDATE_ORDER = "updateOrder";
    // 智能合约查询订单方法名
    private static final String SELECT_ORDER = "selectOrder";

    private final String separator = ",";

    @Autowired
    private IUserExtendService userExtendService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private WeBASEUtils weBASEUtils;

    @Autowired
    private IOrderService orderService;

    /**
     * 新增订单数据
     * @param order 订单
     * @param deptId 部门ID
     * @param optionUserId 订单操作者
     * @return WeBaseResult
     */
    @Override
    public WeBaseResult insertOrder(Order order, Long deptId, Long optionUserId){

        // 查询该煎药机构的合约address
        String contractAddress = deptService.getContractAddress(deptId);
        // 查询该操作员的address
        String userName = userExtendService.getSignUserName(optionUserId);
        // 准备智能合约订单插入方法所需参数

        String param = this.createParam(order, optionUserId);
        List<String> list = new ArrayList<>();
        list.add(param);

        // 调用智能合约
        String string = weBASEUtils.funcPost(userName,contractAddress,INSERT_ORDER, list);
        WeBaseResult weBaseResult = weBASEUtils.convertResult(string);

        return weBaseResult;
    }

    /**
     * 更新订单数据
     * @param order 需更新订单
     * @param deptId 部门ID
     * @param optionUserId 订单操作者
     * @return WeBaseResult
     */
    @Override
    public WeBaseResult updateOrder(Order order, Long deptId, Long optionUserId) {

        // 查询该煎药机构的合约address
        String contractAddress = deptService.getContractAddress(deptId);
        // 查询该操作员的address
        String userName = userExtendService.getSignUserName(optionUserId);
        // 准备智能合约订单插入方法所需参数
        String param = this.createParam(order, optionUserId);
        List<String> list = new ArrayList<>();
        list.add(String.valueOf(order.getId()));
        list.add(param);

        String string = weBASEUtils.funcPost(userName,contractAddress,UPDATE_ORDER, list);
        System.out.println("updateOrder: " + string);
        WeBaseResult weBaseResult = weBASEUtils.convertResult(string);

        return weBaseResult;
    }

    /**
     * 根据Order生成区块链调用所需参数
     * @param order 订单
     * @param optionUserId 订单操作者
     * @return
     */
    private String createParam(Order order, Long optionUserId){
        StringBuffer param = new StringBuffer();
        param.append(order.getId()).append(this.separator)
                .append(order.getUserId()).append(this.separator)
                .append(order.getDeptId()).append(this.separator)
                .append(order.getStatus()).append(this.separator)
                .append(order.getMedicineCount()).append(this.separator)
                .append(order.getResultCount()).append(this.separator)
                .append(order.getPanId()).append(this.separator)
                .append(order.getContainerId()).append(this.separator)
                .append(order.getWeight()).append(this.separator)
                .append(optionUserId);

        return param.toString();
    }

    @Override
    public WeBaseResult selectOrderByOrderId(Long orderId, Long deptId ,Long optionUserId) {

        String contractAddress = deptService.getContractAddress(deptId);
        String userName = userExtendService.getSignUserName(optionUserId);
        List<String> list = new ArrayList<>();
        String param = orderId.toString();
        list.add(param);

        String string = weBASEUtils.funcPost(userName,contractAddress,SELECT_ORDER, list);

        WeBaseResult result = weBASEUtils.convertViewResult(string);
        return result;
    }

}
